﻿using Microsoft.Xaml.Behaviors.Media;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace 无人值守地磅称重系统.Helpers
{
    public class PasswordBoxHelper
    {
        public static string GetPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordProperty);
        }

       public static void SetPassword(DependencyObject obj,string value)
        {
            obj.SetValue(PasswordProperty,value);
        }

        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata("", 
                new PropertyChangedCallback(OnPasswordChanged)));

        private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is PasswordBox passwordBox)
            {
               passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
               passwordBox.PasswordChanged+= PasswordBox_PasswordChanged;
            }
        }
        public static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if(sender is PasswordBox passwordBox)
            {
                SetPassword(passwordBox, passwordBox.Password);
            }
        }
    }
}
